home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Feb / di9802am / ConfZipU.pas
Pascal/Delphi Source File  |  1997-10-02  |  4KB  |  127 lines

  1. {Listing 1}
  2. unit ConfZipU;
  3.  
  4. interface
  5.  
  6. uses
  7.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  8.   StdCtrls, Buttons, AbZipper, AbArcTyp, AbZBrows, AbUnZper;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     Memo1: TMemo;
  13.     Label1: TLabel;
  14.     EncryptAndCompressBtn1: TBitBtn;
  15.     DecryptAndDecompressBtn1: TBitBtn;
  16.     BitBtn1: TBitBtn;
  17.     OpenDialog1: TOpenDialog;
  18.     SaveDialog1: TSaveDialog;
  19.     AbUnZipper1: TAbUnZipper;
  20.     AbZipper1: TAbZipper;
  21.     procedure EncryptAndCompressBtn1Click(Sender: TObject);
  22.     procedure DecryptAndDecompressBtn1Click(Sender: TObject);
  23.   private
  24.     { Private declarations }
  25.     EncryptPassword : string;
  26.   public
  27.     { Public declarations }
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34. uses Abdlgpwd, LbProc, LbCipher;
  35.  
  36. {$R *.DFM}
  37.  
  38. procedure TForm1.EncryptAndCompressBtn1Click(Sender: TObject);
  39. var
  40.   Dlg : TPassWordDlg;
  41.   Key : TKey128;
  42.   EncryptedFile : string;
  43. begin
  44.   if (Memo1.text='') then
  45.     begin
  46.       MessageDlg('You haven''t entered any text to save', mtError,
  47.                    [mbOK], 0);
  48.       exit;
  49.     end;
  50.   Dlg := TPassWordDlg.Create(Application);
  51.   EncryptPassword := '';
  52.   try
  53.     Dlg.ShowModal;
  54.     if Dlg.ModalResult = mrOK then
  55.       EncryptPassword := Dlg.Edit1.Text;
  56.   finally
  57.     Dlg.Free;
  58.   end;
  59.   if EncryptPassword = '' then exit;
  60.     ChDir(ExtractFilePath(Application.ExeName));
  61.     SaveDialog1.Title := 'Enter Name of Text File to Archive';
  62.     SaveDialog1.Filter := 'Text files (*.txt)|*.TXT';
  63.     if SaveDialog1.Execute then
  64.       begin
  65.       if Pos('.', SaveDialog1.FileName)=0 then
  66.          SaveDialog1.FileName := concat(SaveDialog1.FileName, '.txt');
  67.          try
  68.            Memo1.Lines.SaveToFile(ChangeFileExt(SaveDialog1.FileName, '.txt'));
  69.          finally { wrap up }
  70.          end;        { try / finally }
  71.          with AbZipper1 do
  72.            begin
  73.              BaseDirectory := ExtractFilePath(SaveDialog1.FileName);
  74.              AbZipper1.Filename := ChangeFileExt(SaveDialog1.FileName, '.zip');
  75.              EncryptedFile := ChangeFileExt(SaveDialog1.FileName, '.xxx');
  76.              GenerateLMDKey(Key, SizeOf(Key), EncryptPassword);
  77.              LBCEncryptFile(SaveDialog1.FileName, EncryptedFile, Key, 16, True);
  78.              AddFiles(SaveDialog1.FileName, 0);
  79.              Save;
  80.            end;        { with }
  81.          If MessageDlg('Delete Text File?', mtConfirmation, [mbOK, mbCancel], 0)
  82.             =idOK then DeleteFile(ChangeFileExt(SaveDialog1.FileName, '.txt'));
  83.       end;
  84. end;
  85.  
  86. procedure TForm1.DecryptAndDecompressBtn1Click(Sender: TObject);
  87. var
  88.   TextFile : string;
  89.   Dlg : TPassWordDlg;
  90.   Key : TKey128;
  91.   EncryptedFile : string;
  92. begin
  93.  Dlg := TPassWordDlg.Create(Application);
  94.   EncryptPassword := '';
  95.   try
  96.     Dlg.ShowModal;
  97.     if Dlg.ModalResult = mrOK then
  98.       EncryptPassword := Dlg.Edit1.Text;
  99.   finally
  100.     Dlg.Free;
  101.   end;
  102.   if EncryptPassword = '' then exit;
  103.  
  104.     OpenDialog1.Title := 'Open Zip File';
  105.     OpenDialog1.Filter := 'Zip files (*.zip)|*.ZIP';
  106.     if OpenDialog1.Execute then
  107.       begin
  108.         TextFile := ExtractFileName(ChangeFileExt(OpenDialog1.FileName, '.txt'));
  109.         EncryptedFile := ChangeFileExt(OpenDialog1.FileName, '.xxx');
  110.         with AbUnZipper1 do
  111.             begin
  112.               BaseDirectory := ExtractFilePath(OpenDialog1.FileName);
  113.               ChDir(BaseDirectory);
  114.               Filename := OpenDialog1.FileName;
  115.               ExtractFiles(EncryptedFile);
  116.               GenerateLMDKey(Key, SizeOf(Key), EncryptPassword);
  117.               LBCEncryptFile(EncryptedFile, TextFile, Key, 16, False);
  118.             end;        { with }
  119.         try
  120.           Memo1.Lines.LoadFromFile(TextFile);
  121.         finally
  122.         end;
  123.       end;
  124.  end;
  125.  
  126. end.
  127.